package com.lecoauto.e02.hook;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.view.InputEvent;
import android.view.MotionEvent;

import java.util.Objects;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class TouchHook implements IXposedHookLoadPackage {
    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
        if ("android".equals(lpparam.packageName)) {
            hookInjectInputEventInternal(lpparam);
        } else {
            hookApps(lpparam);
        }
    }

    private void hookApps(XC_LoadPackage.LoadPackageParam loadPackageParam) {
        try {
            XposedHelpers.findAndHookMethod(XposedHelpers.findClass("android.app.Activity", loadPackageParam.classLoader),
                    "onCreate", Bundle.class, new XC_MethodHook() {
                        @SuppressLint("UnspecifiedRegisterReceiverFlag")
                        @Override
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            Activity activity = (Activity) param.thisObject;
                            IntentFilter intentFilter = new IntentFilter();
                            intentFilter.addAction("com.lecoauto.e02.event");

                            activity.registerReceiver(new BroadcastReceiver() {
                                @Override
                                public void onReceive(Context context, Intent intent) {
                                    if (intent.getAction() == null || !Objects.equals(intent.getAction(), "com.lecoauto.e02.event")) {
                                        return;
                                    }
                                    XposedBridge.log("[E-TOUCH]：hookApps onReceive--" + loadPackageParam.packageName);
                                    MotionEvent motionEvent = intent.getParcelableExtra("motion_event");
                                    activity.runOnUiThread(() -> activity.dispatchTouchEvent(motionEvent));
                                }
                            }, intentFilter);
                        }
                    });
        } catch (Throwable th) {
            XposedBridge.log("[E-TOUCH]：hookApps error：" + th);
        }
    }

    private void hookInjectInputEventInternal(XC_LoadPackage.LoadPackageParam loadPackageParam) {
        try {
            Class findClass = XposedHelpers.findClass("com.android.server.input.InputManagerService", loadPackageParam.classLoader);
            XposedHelpers.findAndHookMethod(findClass, "injectInputEventInternal", InputEvent.class, Integer.TYPE, Integer.TYPE, new XC_MethodHook() {
                @Override
                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                    Intent intent = new Intent("com.lecoauto.e02.event");
                    intent.putExtra("motion_event", (MotionEvent) param.args[0]);
                    ((Context) XposedHelpers.getObjectField(param.thisObject, "mContext")).sendBroadcast(intent);
                    param.setResult(Boolean.TRUE);
                }
            });
        } catch (Throwable th) {
            XposedBridge.log("[E-TOUCH]：hookInjectInputEventInternal error：" + th);
        }
    }
}
